﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace WFWS.Core
{
    public class HttpRequestHandler : IHttpRequestHandler
    {
        bool _inited = false;
        object _lockObj = new object();

        public void Init(HttpRequest request)
        {
            if (!_inited)
            {
                lock (_lockObj)
                {
                    if (!_inited)
                    {
                        Request = request;
                        Response = new HttpResponse(request);
                        HttpResponseManager.RegisterResponse(Response);
                        _inited = true;
                    }
                }
            }
        }

        public HttpRequestHandler()
        {
        }

        private static Timer _timer;
        public HttpRequest Request { get; private set; }
        public HttpResponse Response { get; private set; }

        #region IHttpRequestHandler Members
        const int _bufferSize = 4096;
        byte[] _buffer = new byte[_bufferSize];

        private static int count = 0;

        public void Execute(HttpRequest request)
        {
            if (!_inited)
                Init(request);

            AddHead();
            AddBody(count);
            _timer = new Timer(AddBody, count, 10, 1000);
            //return Response;
        }


        private void AddHead()
        {
            Response.SendStatusLine(200, "OK");
        }

        private void AddBody(object state)
        {
            int count = (int)state;
            var message = Encoding.UTF8.GetBytes("hello");
            Response.SendBody(message, 0, message.Length);
            Response.Flush(false);

            if (count >= 10)
            {
                Response.Flush(true);
                _timer.Dispose();
            }
        }

        /*
        public IHttpRequestParser Parser
        {
            get
            {
                if (_parser == null)
                {
                    _parser = new HttpRequestParser(_stream);
                    _parser.HeaderReceived += new EventHandler<HeaderEventArgs>(Parser_HeaderReceived);
                    _parser.RequestCompleted += new EventHandler(Parser_RequestCompleted);
                    _parser.RequestLineReceived += new EventHandler<RequestLineEventArgs>(Parser_RequestLineReceived);
                }
                return _parser;
            }
        }

        void Parser_RequestLineReceived(object sender, RequestLineEventArgs e)
        {
            Request.Method = e.HttpMethod;
            Request.HttpVersion = e.HttpVersion;
            Request.Path = e.UriPath;
            //TODO: //how to solve?
            //Request.Uri = new Uri(e.UriPath);
        }

        void Parser_RequestCompleted(object sender, EventArgs e)
        {
            var bytes = Encoding.UTF8.GetBytes("hello!");
            Response.Body = _stream;
            Response.Body.Write(bytes, 0, bytes.Length);

            Response.Send();
            Response.Flush();

            _stream.Close();
            _socket.Close();
        }

        void Parser_HeaderReceived(object sender, HeaderEventArgs e)
        {
            Request.Headers.Set(new HttpHeader(e.Name,e.Value));
            if (e.Name == "Cookie")
            {
                var cookies = HttpRequestParser.ParseCookie(e.Value);
                Request.Cookies = cookies;
            }
        }
        */
        #endregion

    }
}
